<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/4/18 12:31
 */

namespace App\Components\System;


use App\Components\Logs\LogAgent;
use App\Exception\ServiceException;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\ClientFactory;
use Psr\Http\Message\ResponseInterface;

class HttpClient
{

    #[Inject]
    protected ClientFactory $clientFactory;


    protected $headers = [];


    /**
     * @var Client
     */
    protected $client;



    protected $cookies = [];

    /**
     * 不需要验证证书
     * @var bool
     */
    protected $verify = false;


    /**
     * 请求超时时间
     * @var int
     */
    protected $timeout = 10;




    public function __construct(array $headers = [], int $timeout = 10, bool $verify = false)
    {

        $this->verify = $verify;
        $this->timeout = $timeout;
        $this->client = $this->clientFactory->create([
            'timeout' => $this->timeout,
            'verify' => $this->verify,
            'headers' => $headers
        ]);
    }


    /**
     * @param string $url
     * @param string $bodyStr
     * @return ResponseInterface
     * @throws GuzzleException
     */
    public function postRaw(string $url,string $bodyStr)
    {
        try {

            $response = $this->client->post($url, [
                RequestOptions::BODY => $bodyStr
            ]);

            return $response;
        }catch (ClientException $e){
            $error = [
                'error_msg' => $e->getMessage(),
                'bodyStr' => $bodyStr
            ];
            LogAgent::getInstance('http','POST-RAW')->info(json_encode($error,JSON_UNESCAPED_UNICODE));

            throw new ServiceException($e->getMessage());
        }
    }

    /**
     * post json 模式请求
     * @param string $url
     * @param array $params
     * @return ResponseInterface
     */
    public function postForm(string $url,array $params,string $postOption = RequestOptions::FORM_PARAMS): ResponseInterface
    {
        try {
            $requestData = [
                $postOption => $params
            ];

            if(count($this->cookies))
            {
                $domain = parse_url($url)['host'];
                $requestData['cookies'] = CookieJar::fromArray($this->cookies,$domain);
            }

            $response = $this->client->post($url,$requestData);

            if($response->getStatusCode() != 200)
            {
                $infoMsg = json_encode([
                    'request' => $url,
                    'params' => $params,
                    'response' => $response->getBody()->getContents(),
                ],JSON_UNESCAPED_UNICODE);
                LogAgent::getInstance('http','postJson')->info($infoMsg);
                throw new ServiceException("response code error");
            }
            return $response;
        }catch (ClientException $e)
        {
            $infoMsg = json_encode([
                'request' => $url,
                'params' => $params,
                'ClientException' => $e->getMessage(),
            ],JSON_UNESCAPED_UNICODE);
            LogAgent::getInstance('http','postJson')->error($infoMsg);
            throw new ServiceException($e->getMessage());
        }catch (GuzzleException $e)
        {
            $infoMsg = json_encode([
                'request' => $url,
                'params' => $params,
                'GuzzleException' => $e->getMessage(),
            ],JSON_UNESCAPED_UNICODE);
            LogAgent::getInstance('http','postJson')->error($infoMsg);
            throw new ServiceException($e->getMessage());
        }

    }
}